import request from '@/utils/request' 
import { unix } from 'moment'
const state = {
  projectList: [],
  project: {}, //当前选中的project
  today: { // 当前项目的今日【用电】、【用水量】
    power: 0,
    water: 0,
    powerUsage: {}, // 今日的分项电能
  },
  powerUsage: {}, // 分项电能
}

const mutations = {
  SET_PROJECT_ARR: (state, data) => {
    state.projectList = data
  },
  SET_PROJECT: (state, data) => {
    state.project = data
    uni.setStorageSync('location', data.location)
  },
  SET_TOTAY: (state, data) => {
    state.today = { ...state.today, ...data }
  },
  SET_POWER_USAGE: (state, data) => {
    state.powerUsage = data
  }
}

const actions = {
  getProjectList({ commit }, data) {
    return request('/api/project/list/user').then(res => {
      commit('SET_PROJECT_ARR', res.result)
    })
  },
  setProject({ commit }, data) {
    commit('SET_PROJECT', data)
  },
  fetchTodayWaterAndPower({ commit }, projectId ) {
    request('/api/statistics/waterPower/real', { projectId }).then((res) => {
      commit('SET_TOTAY', res?.result)
    })
  },
  fetchPowerUsage({ commit }, projectId ) {
    request('/api//energy/consumption/usage/power', { projectId }, 'POST').then((res) => {
      commit('SET_POWER_USAGE', res?.result)
    })
  },
  fetchTodayPowerUsage({ commit }, projectId ) {
    request('/api/energy/consumption/usage/power/Today', { projectId }, 'POST').then((res) => {
      commit('SET_POWER_USAGE', res?.result)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
